#include "cmysig.h"
#include <QDebug>
#include <QFile>
#include <QHash>
#include "channelad.h"
#include <string.h>

extern QStringList *pmychannelNames;
extern QHash<QString, ChannelAD> *pgchannels;
void writeFileLines(QString fp,QStringList ls)
{
    QFile f(fp);
    f.open(QIODevice::WriteOnly);
    QTextStream stream(&f);
    int i=0;
    for (i=0;i<ls.size();i++)
    {
        stream<<ls[i]+'\n';
    }
}
QStringList readLines(QString fp)
{
    QFile f(fp);
    f.open(QIODevice::ReadOnly);
    QTextStream stream(&f);
    QString line;
    QStringList ls;
    do {
        line = stream.readLine();
        ls.append(line);
    } while (!line.isNull());

    return ls;
}
void CMySig::toggle(int i,bool b)
{
    (curves+i)->setVisible(b);
}
CMySig::CMySig()
{
    name="";
    weight=0.123;
    xn=0;
    numChannels=4;
    channels=new QByteArray[numChannels];
    curves=new QwtPlotCurve[numChannels];
//    QColor c0(QRgb((*pgchannels)["C"].color));
//    qDebug()<<c0
//    (curves+0)->setPen(QPen(c0));
//    QColor c1(QRgb((*pgchannels)["S"].color));
//    (curves+1)->setPen(QPen(c1));
//    QColor c2(QRgb((*pgchannels)["S"].color));
//    (curves+2)->setPen(QPen(c2));
//    QColor c3(QRgb((*pgchannels)["S"].color));
//    (curves+3)->setPen(QPen(c3));
    int n=pmychannelNames->size();
    for (int i=0;i<n;i++)
    {
        QColor c(QRgb((*pgchannels)[(*pmychannelNames)[i]].color));
        qDebug()<<c;
        (curves+i)->setTitle((*pmychannelNames)[i]);
        (curves+i)->setPen(c);
    }

    //(curves+2)->setPen(QPen(QColor((*pgchannels)["C"].color)));
    //(curves+3)->setPen(QPen(QColor((*pgchannels)["C"].color)));

}
CMySig::~CMySig()
{
    delete[] channels;
    delete[] curves;
}
//void showd(double * x,double * y ,long n){
//    for(int i=0;i<n;i++){
//        qDebug()<<*(x+i);
//        qDebug()<<*(y+i);
//    }

//}
void CMySig::show(QwtPlot *plot)
{
    if(xn>0)
    {
        for(int i=0;i<numChannels;i++)
        {
            (curves+i)->attach(plot);
//            showd((double *)mtime.constData(), (double *)channels[i].constData(), xn);
            (curves+i)->setRawSamples((double *)mtime.constData(), (double *)channels[i].constData(), xn);
        }
    }
}
bool CMySig::haveData()
{
    return xn>0;
}
//void CMySig::setTimeArr(QByteArray timeArr)
//{
//    mtime=timeArr;
//}
void CMySig::setXn(long n)
{
    xn=n;
    mtime.resize(xn*8);
    for (long i=0;i<numChannels;i++)
    {
        channels[i].resize(xn*8);//double
    }
}
QStringList CMySig::toStringList()
{
    QStringList l;
    if(xn>0)
    {
        qDebug()<<xn;
        double *p;
        double * volt[numChannels];
        p=(double *)mtime.constData();
        for(int j=0;j<numChannels;j++)
        {
            volt[j]=(double *)channels[j].constData();
        }
        for(long i=0;i<xn;i++)
        {
            QString voltStr,tmpStr;
            //voltStr=tmpStr.setNum(i);
            voltStr=tmpStr.setNum(*p);
            for(int j=0;j<numChannels;j++)
            {
                voltStr+="\t"+tmpStr.setNum(*volt[j]);
            }
            p++;
            for(int j=0;j<numChannels;j++)
            {
                volt[j]++;
            }
            qDebug()<<voltStr;
            l.append(voltStr);
        }
    }
    return l;
}

//void CMySig::setVoltArr(QByteArray voltArr, int index)
//{
//    if (index<numChannels && index>=0)
//    {
//        channels[index]=voltArr;
//    }
//}
void CMySig::writeFile(QString fp)
{
    QFile f(fp);
    f.open(QIODevice::WriteOnly);
    QDataStream stream(&f);
    qint8 tmp;
    tmp=xn;
    stream<<tmp;
    double *p;
    double * volt[numChannels];
    p=(double *)mtime.constData();
    for(int j=0;j<numChannels;j++)
    {
        volt[j]=(double *)channels[j].constData();
    }
    for(long i=0;i<xn;i++)
    {
        stream<<*p;
        for(int j=0;j<numChannels;j++)
        {
            stream<<*volt[j];
        }
        p++;
        for(int j=0;j<numChannels;j++)
        {
            volt[j]++;
        }
    }
    f.close();
}
void CMySig::testData()
{
    name="hello";
    weight=0.123;
    xn=100;
    mtime.resize(xn*sizeof(double));
    for (long i=0;i<numChannels;i++)
    {
        channels[i].resize(xn*sizeof(double));//double
    }
    double *p;
    double * volt[numChannels];
    p=(double *)mtime.constData();
    for(int j=0;j<numChannels;j++)
    {
        volt[j]=(double *)channels[j].constData();
    }
    for(long i=0;i<xn;i++)
    {
        *p=i;
        for(int j=0;j<numChannels;j++)
        {
            *volt[j]=i*((j+1.0)/numChannels);
        }
        p++;
        for(int j=0;j<numChannels;j++)
        {
            volt[j]++;
        }
    }

}
void CMySig::readFile(QString fp)
{
    QFile f(fp);
    f.open(QIODevice::ReadOnly);
    qint8 tmp;
    QDataStream stream(&f);
    stream>>tmp;
    xn=tmp;
    qDebug()<<xn;
    int i=0;
    mtime.resize(xn*8);
    for (i=0;i<numChannels;i++)
    {
        channels[i].resize(xn*8);
    }
    double *p;
    double * volt[numChannels];
    p=(double *)mtime.constData();
    for(int j=0;j<numChannels;j++)
    {
        volt[j]=(double *)channels[j].constData();
    }
    for(long i=0;i<xn;i++)
    {
        QString voltStr,tmpStr;
        stream>>*p;
        for(int j=0;j<numChannels;j++)
        {
            stream>>*volt[j];
        }
        p++;
        for(int j=0;j<numChannels;j++)
        {
            volt[j]++;
        }
    }
    f.close();
}
void CMySig::readTextFile(QString fp){
    QStringList lines=readLines(fp);
    name=lines[0];
    weight=lines[1].toDouble();
    xn=lines.size()-3;
    mtime.resize(xn*8);
    for (long i=0;i<numChannels;i++)
    {
        channels[i].resize(xn*8);
    }
    double *p;
    double * volt[numChannels];
    p=(double *)mtime.constData();
    for(int j=0;j<numChannels;j++)
    {
        volt[j]=(double *)channels[j].constData();
    }
    for(long i=2;i<xn+2;i++)
    {
        QString line=lines[i];
        QStringList ws=line.split("\t");
        qDebug()<<line;
        qDebug()<<ws;
        if (ws.size()<5)
        {
            qDebug()<<"no enough channels";
        }
        else
        {
            *p=ws[0].toDouble();
            for(int j=0;j<numChannels;j++)
            {
                *volt[j]=ws[1+j].toDouble();
            }
            p++;
            for(int j=0;j<numChannels;j++)
            {
                volt[j]++;
            }
        }
    }
    qDebug()<<xn;    
}
void CMySig::readTextFile_new(QString fp)
{
    // char * ch;
    // ch=fp.toLatin1().data();
    // printf("%s",ch);
    qDebug()<<fp;
    QStringList lines=readLines(fp);
    // qDebug()<<lines;
    name=lines[2];
    weight=lines[1].toDouble();
    xn=lines.size()-11;
    mtime.resize(xn*8);
    for (long i=0;i<numChannels;i++)
    {
        channels[i].resize(xn*8);
    }
    double *p;
    double * volt[numChannels];
    p=(double *)mtime.constData();
    for(int j=0;j<numChannels;j++)
    {
        volt[j]=(double *)channels[j].constData();
    }
    for(long i=4;i<xn+4;i++)
    {
        QString line=lines[i];
        QStringList ws=line.split(",");
        qDebug()<<line;
        qDebug()<<ws;
        if (ws.size()<5)
        {
            qDebug()<<"no enough channels";
        }
        else
        {
            *p=ws[0].toDouble();
            for(int j=0;j<numChannels;j++)
            {
                *volt[j]=ws[1+j].toDouble();
                qDebug("%lf",*volt[j]);
            }
            p++;
            for(int j=0;j<numChannels;j++)
            {
                volt[j]++;
            }
        }
    }
    qDebug()<<xn;
}
void CMySig::writeTextFile(QString fp)
{
    QFile f(fp);
    f.open(QIODevice::WriteOnly);
    QTextStream stream(&f);
    stream<<name<<"\n";//name
    stream<<weight<<"\n";//weight
    QStringList l;
    l=this->toStringList();
    for(long i=0;i<l.size();i++)
    {
        stream<<l[i]<<"\n";
    }
    f.close();
}
void CMySig::saveSig(long id)
{
    QSqlRecord record;

    QSqlField f1("sampleid", QVariant::Int);
    QSqlField f2("mtimeArr", QVariant::ByteArray);
    QSqlField f3("cvoltArr", QVariant::ByteArray);
    QSqlField f4("svoltArr", QVariant::ByteArray);
    QSqlField f5("c2voltArr", QVariant::ByteArray);
    QSqlField f6("s2voltArr", QVariant::ByteArray);
    QSqlField f7("xn", QVariant::Int);

    f1.setValue(QVariant((int)id));
    f2.setValue(QVariant(mtime));
    f3.setValue(QVariant(channels[0]));
    f4.setValue(QVariant(channels[1]));
    f5.setValue(QVariant(channels[2]));
    f6.setValue(QVariant(channels[3]));
    f7.setValue(QVariant((int)xn));

    record.append(f1);
    record.append(f2);
    record.append(f3);
    record.append(f4);
    record.append(f5);
    record.append(f6);
    record.append(f7);

    QSqlTableModel* model = new QSqlTableModel(NULL);
    model->setTable("voltnew");
    model->insertRecord(-1, record);
}
void CMySig::loadSig(long id)
{
    QString cmd;
    QString idstr;
    idstr.setNum(id);
    cmd="SELECT * FROM voltnew where sampleid="+idstr;
    qDebug()<<cmd;
    QSqlQuery q(cmd);
    QSqlRecord rec;// = q.record();
    while (q.next())
    {
        rec = q.record();
        xn=(rec.value("xn").toInt());
        mtime=rec.value("mtimeArr").toByteArray();
        channels[0]=rec.value("cvoltArr").toByteArray();
        channels[1]=rec.value("svoltArr").toByteArray();
        channels[2]=rec.value("c2voltArr").toByteArray();
        channels[3]=rec.value("s2voltArr").toByteArray();
    }
}
void CMySig::saveAll()
{
}
void CMySig::setOneData(QList<double> l,long at)
{
    if (at<0) return;
    if (at>=0 && at<xn)
    {
        double *p;
        double * volt[numChannels];
        p=(double *)mtime.constData();
        for(int j=0;j<numChannels;j++)
        {
            volt[j]=(double *)channels[j].constData();
        }

        *(p+at)=l[0];
        for(int j=0;j<numChannels;j++)
        {
            *(volt[j]+at)=l[j+1];
        }
    }
    else
    {
            setXn(at+1);
            double *p;
            double * volt[numChannels];
            p=(double *)mtime.constData();
            for(int j=0;j<numChannels;j++)
            {
                volt[j]=(double *)channels[j].constData();
            }

            *(p+at)=l[0];
            for(int j=0;j<numChannels;j++)
            {
                *(volt[j]+at)=l[j+1];
            }
    }
}
long CMySig::getXn()
{
    return xn;
}
void CMySig::detach()
{
    if(xn>0)
    {
        for(int i=0;i<numChannels;i++)
        {
            (curves+i)->detach();
        }
    }
}
void CMySig::clone(CMySig *sig2)
{
    qDebug()<<sig2->getXn();
    setXn(sig2->getXn());
    void *des;void *src;
    des=(void *)mtime.constData();
    src=(void *)sig2->mtime.constData();
    memcpy(des,src,xn*sizeof(double));
    for (long i=0;i<numChannels;i++)
    {
        //channels[i].resize(xn*8);
        des=(void *)channels[i].constData();
        src=(void *)sig2->channels[i].constData();
        memcpy(des,src,xn*sizeof(double));

    }
}
